package com.simon.annual.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.jsoup.helper.StringUtil;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import com.simon.annual.entity.ReportEmployee;
import com.simon.annual.entity.ReportManagement;
import com.simon.annual.entity.ReportNeeq;
import com.simon.annual.repositoy.IReportEmployeeRepositoy;
import com.simon.annual.repositoy.IReportManagementRepositoy;
import com.simon.annual.repositoy.IReportNeeqRepositoy;
import com.simon.annual.service.ICompanyPublicInfo;
import com.simon.annual.service.IManagement;

/**
 * 第八节 董事、监事、高级管理人员及员工情况
 * */
@Service
public class FindManagementImpl implements IManagement{

	@Resource
	private ICompanyPublicInfo cmpanyPublicInfo;
	@Resource
	private IReportManagementRepositoy reportManagementRepositoy;
	@Resource
	private IReportEmployeeRepositoy reportEmployeeRepositoy;
	@Resource
	private IReportNeeqRepositoy reportNeeqRepositoy;
	@Resource
	private Environment env;
	
	@Override
	public ReportNeeq analysis(Document doc, ReportNeeq reportNeeq, int companyPtrId) {
		//基本情况
		Elements  title = doc.getElementsMatchingOwnText(env.getProperty("pdf.eight"));
		Element management=null;
		for (Element element : title) {
			if(element.text().length()>18&&element.text().length()<25){
				management =cmpanyPublicInfo.getParentTag(element,element);
			}
		}
		Element managementT = cmpanyPublicInfo.findNextByChar(env.getProperty("pdf.eight.management"), management, 0);
		managementT = cmpanyPublicInfo.getNextTag(managementT, managementT);
		Elements managementTr = managementT.select("table tr");
		List<ReportManagement> reportManagementList = new ArrayList<ReportManagement>();
		int index =0;
		if(managementT.getElementsMatchingOwnText(env.getProperty("pdf.eight.relationship")).hasText()){
			index=1;
		}
		if(managementTr.size()>1){
			for (int i = 1; i < managementTr.size()-(3+index); i++) {
				Elements managementTd = managementTr.get(i).children();
				ReportManagement reportManagement = new ReportManagement();
				reportManagement.setName(managementTd.get(0).text());
				reportManagement.setTitle(managementTd.get(1).text());
				reportManagement.setGender(managementTd.size()>3?managementTd.get(2).text():"");
				reportManagement.setAge(managementTd.size()>3?managementTd.get(3).text():"");
				reportManagement.setEduBackground(managementTd.size()>4?managementTd.get(4).text():"");
				reportManagement.setTermOfService(managementTd.size()>5?managementTd.get(5).text():"");
				reportManagement.setIsPaidSalary(managementTd.size()>6?managementTd.get(6).text():"");
				reportManagement.setProjectScopeId(companyPtrId);
				reportManagementList.add(reportManagement);
			}
			//董事会人数
			Elements boardMember = managementTr.get(managementTr.size()-(3+index)).children();
			reportNeeq.setBoardMemberNo(StringUtil.isBlank(boardMember.last().text().replace("-", ""))?boardMember.first().text().replaceAll("\\D+", ""):boardMember.last().text());
			Elements supervisor = managementTr.get(managementTr.size()-(2+index)).children();
			reportNeeq.setSupervisorMeetingNo(StringUtil.isBlank(supervisor.last().text().replace("-", ""))?supervisor.first().text().replaceAll("\\D+", ""):supervisor.last().text());
			Elements senior = managementTr.get(managementTr.size()-(1+index)).children();
			reportNeeq.setSeniorManagerNo(StringUtil.isBlank(senior.last().text().replace("-", ""))?senior.first().text().replaceAll("\\D+", ""):senior.last().text());
		}
		//董事、监事、高级管理人员相互间关系及与控股股东、实际控制人间关系
		if(index>0){
			reportNeeq.setRelationshipOfManagers(managementTr.last().text());
		}else{
			Element relationship = cmpanyPublicInfo.findNextByChar(env.getProperty("pdf.eight.relationship"), management, -3);
			if(relationship!=null){
				Element relationshipOfManagers = cmpanyPublicInfo.findEndByChar(env.getProperty("pdf.eight.relationship.end"), relationship, "",0);
				reportNeeq.setRelationshipOfManagers(relationshipOfManagers.text());
			}
		}
		//持股情况
		Element stockManagement = cmpanyPublicInfo.findNextByChar(env.getProperty("pdf.eight.stockManagement"), management, -15);
		if(stockManagement!=null){
			Element stockManagementT = cmpanyPublicInfo.getNextTag(stockManagement, stockManagement);
			Elements trs = stockManagementT.select("table tr");
			for (int i = 1; i < trs.size()-1; i++) {
				Elements managementTd = trs.get(i).children();
				if(managementTd.size()>=7){
					ReportManagement reportManagement = new ReportManagement();
					reportManagement.setName(managementTd.get(0).text());
					reportManagement.setTitle(managementTd.get(1).text());
					reportManagement.setBgnCommonShares(managementTd.get(2).text());
					reportManagement.setCommonSharesChange(managementTd.get(3).text());
					reportManagement.setEndCommonShares(managementTd.get(4).text());
					reportManagement.setEndCommonSharePct(managementTd.get(5).text());
					reportManagement.setEndShareOption(managementTd.get(6).text());
					reportManagement.setProjectScopeId(companyPtrId);
					reportManagementList.add(reportManagement);
				}
			}
			reportManagementRepositoy.save(reportManagementList);
		}
		//（三）变动情况
		Element changed = cmpanyPublicInfo.findNextByChar(env.getProperty("pdf.eight.changed"), management, -25);
		if(changed!=null){
			Element changedT = cmpanyPublicInfo.getNextTag(changed, changed);
			Elements changedTd = changedT.select("table tr > *");
			reportNeeq.setIsChairmanChanged(changedTd.get(2).text());
			reportNeeq.setIsGeneralManagerChanged(changedTd.get(4).text());
			reportNeeq.setIsBoardSecretaryChanged(changedTd.get(6).text());
			reportNeeq.setIsFinancialControllerChanged(changedTd.size()>8?changedTd.get(8).text():"");
		}
		//本年新任董事、监事、高级管理人员简要职业经历
		Element experience = cmpanyPublicInfo.findNextByChar(env.getProperty("pdf.eight.experience"), management, -33);
		if(experience!=null){
			Element workExperienceOfSeniorManagers = cmpanyPublicInfo.findEndByChar(env.getProperty("pdf.eight.experience.end"), experience, "",0);
			reportNeeq.setWorkExperienceOfSeniorManagers(workExperienceOfSeniorManagers.text());
		}
		//员工情况按工作性质分类
		Element employeeDep = cmpanyPublicInfo.findNextByChar(env.getProperty("pdf.eight.employeeDep"), management, -45);
		Element employeeDepT = cmpanyPublicInfo.getNextTag(employeeDep, employeeDep);
		Elements employeeDepTr = employeeDepT.select("table tr");
		List<ReportEmployee> reportEmployeeList = new ArrayList<ReportEmployee>();
		for (int i = 1; i < employeeDepTr.size()-1; i++) {
			Elements employeeDepTd = employeeDepTr.get(i).children();
			ReportEmployee reportEmployee = new ReportEmployee();
			reportEmployee.setGroup(employeeDepTd.get(0).text());
			reportEmployee.setBgnNo(employeeDepTd.get(1).text());
			reportEmployee.setEndNo(employeeDepTd.get(2).text());
			reportEmployee.setProjectScopeId(companyPtrId);
			reportEmployee.setGroupBy("dep");
			reportEmployeeList.add(reportEmployee);
		}
		//员工情况按教育程度分类
		Element employeeEdu = cmpanyPublicInfo.findNextByChar(env.getProperty("pdf.eight.employeeEdu"), employeeDep, -15);
		if(employeeEdu!=null){
			Element employeeEduT = cmpanyPublicInfo.getNextTag(employeeEdu, employeeEdu);
			Elements employeeEduTr = employeeEduT.select("table tr");
			for (int i = 1; i < employeeEduTr.size()-1; i++) {
				Elements employeeEduTd = employeeEduTr.get(i).children();
				ReportEmployee reportEmployee = new ReportEmployee();
				reportEmployee.setGroup(employeeEduTd.get(0).text());
				reportEmployee.setBgnNo(employeeEduTd.get(1).text());
				reportEmployee.setEndNo(employeeEduTd.get(2).text());
				reportEmployee.setProjectScopeId(companyPtrId);
				reportEmployee.setGroupBy("edu");
				reportEmployeeList.add(reportEmployee);
			}
		}
		//人员变动、人才引进、培训、招聘、薪酬政策、需公司承担费用的离退休职工人数等情况
		Element hr = cmpanyPublicInfo.findNextByChar(env.getProperty("pdf.eight.hrReview"), employeeDep, -23);
		if(hr!=null){
			Element hrReview = cmpanyPublicInfo.findEndByChar(env.getProperty("pdf.eight.hrReview.end"), hr, "",0);
			reportNeeq.setHrReview(hrReview.text());
		}
		//核心员工
		Element employeeCore = cmpanyPublicInfo.findNextByChar(env.getProperty("pdf.eight.employeeCore"), employeeDep, -35);
		if(employeeCore!=null){
			Element employeeCoreT = cmpanyPublicInfo.getNextTag(employeeCore, employeeCore);
			Elements employeeCoreTr = employeeCoreT.select("table tr");
			for (int i = 1; i < employeeCoreTr.size(); i++) {
				Elements employeeCoreTd = employeeCoreTr.get(i).children();
				if(employeeCoreTd.size()==5){
					ReportEmployee reportEmployee = new ReportEmployee();
					reportEmployee.setGroup(employeeCoreTd.get(0).text());
					reportEmployee.setBgnNo(employeeCoreTd.get(1).text());
					reportEmployee.setEndNo(employeeCoreTd.get(2).text());
					reportEmployee.setEndCommonShares(employeeCoreTd.get(3).text());
					reportEmployee.setEndCommonOption(employeeCoreTd.get(4).text());
					reportEmployee.setProjectScopeId(companyPtrId);
					reportEmployee.setGroupBy("core");
					reportEmployeeList.add(reportEmployee);
				}
			}
		}
		reportEmployeeRepositoy.save(reportEmployeeList);
		//核心技术团队或关键技术人员的基本情况及变动情况
		Element coreEmployee = cmpanyPublicInfo.findNextByChar(env.getProperty("pdf.eight.coreEmployeeReview"), employeeDep, -53);
		if(coreEmployee!=null){
			Element coreEmployeeReview = cmpanyPublicInfo.findEndByChar(env.getProperty("pdf.eight.coreEmployeeReview.end"), coreEmployee, "",0);
			reportNeeq.setCoreEmployeeReview(coreEmployeeReview.text());
		}
		return reportNeeqRepositoy.save(reportNeeq);
	}
}
